<template>
  <div class="app-container">
    <!-- 添加或修改打印模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入模板名称" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="模板类型" prop="templetType">
          <el-select v-model="form.templetType" placeholder="请选择模板类型" :disabled="disabled">
            <el-option
              v-for="item in dict.print_templet_type"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="模板宽度" prop="width">
          <el-input v-model.number="form.width" placeholder="请输入模板宽度" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="模板高度" prop="height">
          <el-input v-model.number="form.height" placeholder="请输入模板高度" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="纸张宽度" prop="pageWidth">
          <el-input v-model.number="form.pageWidth" placeholder="请输入纸张宽度" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="纸张高度" prop="pageHeight">
          <el-input v-model.number="form.pageHeight" placeholder="请输入纸张高度" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status" :disabled="disabled">
            <el-radio
              v-for="item in dict.sys_normal_disable"
              :key="item.dictValue"
              :label="item.dictValue"
            >{{item.dictLabel}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="disabled"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {getPrintTemplet, addPrintTemplet, updatePrintTemplet} from "@/api/system/printTemplet";

  export default {
    name: "PrintTempletFrom",
    dicts: ['print_templet_type', 'sys_normal_disable',],
    data() {
      return {
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //是否禁用
        disabled: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          title: [
            { required: true, message: "模板名称不能为空", trigger: "blur" }
          ],
          templetType: [
            { required: true, message: "模板类型不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {

    },
    methods: {
      show(option) {
        this.reset();
        this.disabled = option.type == 'VIEW'
        if (option.type == 'ADD') {
          this.open = true;
          this.title = "新增打印模板";
        }if (option.type == 'COPY') {
          this.open = true;
          this.title = "复制新增打印模板";
          this.form = option.form
        }  else if (option.type == 'UPDATE' || option.type == 'VIEW') {
          getPrintTemplet(option.id).then(response => {
            this.form = response.data;

            if (option.type == 'VIEW') {
              this.title = "查看打印模板";
            } else {
              this.title = "修改打印模板";
            }
            this.open = true;
          });
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
        this.$emit("cancel")
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          title: null,
          templetType: null,
          width: null,
          height: null,
          pageWidth: null,
          pageHeight: null,
          tempContent: null,
          status: "0",
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updatePrintTemplet(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.cancel()
              });
            } else {
              addPrintTemplet(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.cancel()
              });
            }
          }
        });
      }
    }
  };
</script>
